package com.qf.electronic.mall.cart.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qf.cart.api.ICartService;
import com.qf.constant.CartConstant;
import com.qf.constant.CookieConstant;
import com.qf.dto.ResultBean;
import com.qf.entity.TUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
@RequestMapping("cart")
public class CartController {
    @Reference
    private ICartService cartService;

    @RequestMapping("add/{productId}/{count}")
    @ResponseBody
    public ResultBean addProduct(@CookieValue(name = CookieConstant.USER_CART, required = false) String uuid,
                                 @PathVariable Long productId,
                                 @PathVariable int count,
                                 HttpServletResponse response,
                                 HttpServletRequest request){



        Object obj = request.getAttribute("user");
        if(obj!=null){
            TUser user = (TUser) obj;
            Long userId = user.getId();
            return cartService.addProduct(userId.toString(),productId,count);

        }

        if(uuid==null||"".equals(uuid)){
            uuid = UUID.randomUUID().toString();
            Cookie cookie = new Cookie(CookieConstant.USER_CART,uuid);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        ResultBean resultBean = cartService.addProduct(uuid,productId,count);

        return resultBean;
    }

    @RequestMapping("clean")
    @ResponseBody
    public ResultBean cleanCart(@CookieValue(name=CookieConstant.USER_CART,required = false)String uuid,HttpServletResponse response,
                                HttpServletRequest request){
        Object obj = request.getAttribute("user");
        if(obj!=null){

            TUser user = (TUser) obj;
            return cartService.cleanCart(user.getId().toString());
        }

        if(uuid!=null&&!"".equals(uuid)){

            Cookie cookie = new Cookie(CookieConstant.USER_CART,"");
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);

            return cartService.cleanCart(uuid);
        }
        return ResultBean.error(CartConstant.CART_NULL_MSG);
    }

    @RequestMapping("update/{productId}/{count}")
    @ResponseBody
    public ResultBean updateCart(
            @PathVariable Long productId,
            @PathVariable int count,
            @CookieValue(name=CookieConstant.USER_CART,required = false)String uuid,
            HttpServletRequest request
    ){
        Object obj = request.getAttribute("user");
        if(obj!=null){
            TUser user = (TUser) obj;
            return cartService.updateCart(user.getId().toString(),productId,count);
        }
        return cartService.updateCart(uuid,productId,count);
    }

    @RequestMapping("show")
    @ResponseBody
    public ResultBean showCart(@CookieValue(name=CookieConstant.USER_CART,required = false)String uuid,HttpServletRequest request){

        Object obj = request.getAttribute("user");
        if(obj!=null){
            TUser user = (TUser) obj;
            Long userId = user.getId();
            return cartService.showCart(userId.toString());
        }
        return cartService.showCart(uuid);
    }

    @RequestMapping("merge")
    @ResponseBody
    public ResultBean merge(@CookieValue(name = CookieConstant.USER_CART,required = false)String uuid,
                            HttpServletRequest request,HttpServletResponse response){
        TUser user = (TUser) request.getAttribute("user");
        String userId = null;
        if(user!=null){
            userId = user.getId().toString();
        }

        Cookie cookie = new Cookie(CookieConstant.USER_CART,"");
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return cartService.mergeCart(uuid,userId);
    }
}
